<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>销售订单审批</title>
    <script type="text/javascript">
      var saleOrderAuditWin;
      var saleOrderAuditForCancelWin;
      var saleOrderAuditForEditWin
      var saleOrderId;
      var unsafeDiscountAudit = '<%=session[:unsafe_discount_audit]%>';
      Ext.onReady(function(){      
        var customer = Ext.create('Ext.form.ComboBox', {
          fieldLabel: '客户',
          store: customerStore,
          valueField: 'id',
          displayField: 'name',
          name: 'customer_id',
          loadingText: '搜索中...',
          pageSize: 10,          
          minChars: 0,
          anchor: '100%',
          listConfig: {
            getInnerTpl: function() {
              return '<div data-qtip="{number}">{name}</div>';
            }
          }
        });

        var orderNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '单号',
          name: 'order_number',
          anchor: '100%'
        });

        var auditingStatus = Ext.create('Ext.form.ComboBox', {
          fieldLabel: '审批状态',
          store: auditStatusStore,
          displayField:'text',
          typeAhead: true,
          queryMode: 'local',
          name: 'auditing_status',
          editable:false,
          valueField: 'value',
          anchor: '100%'
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var submitButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          anchor: '30%',
          handler: function(){
            saleOrderAuditDS.removeAll();
            saleOrderAuditDS.getProxy().extraParams.order_number = orderNumber.getValue();
            saleOrderAuditDS.getProxy().extraParams.customer_id = customer.getValue();
            saleOrderAuditDS.getProxy().extraParams.auditing_status = auditingStatus.getValue();
            saleOrderAuditDS.getProxy().extraParams.begin_date = beginDate.getRawValue();
            saleOrderAuditDS.getProxy().extraParams.end_date = endDate.getRawValue();
            saleOrderAuditDS.getProxy().extraParams.start = 0;
            saleOrderAuditDS.load();
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 60,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {anchor: '100%'},
              title: '查询条件',
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  anchor: '100%',
                  items: [{
                      columnWidth: .17,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [orderNumber]
                    }, {
                      columnWidth: .17,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [auditingStatus]
                    }, {
                      columnWidth: .3,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [customer]
                    }]
                },{
                  layout: 'column',
                  xtype: 'container',
                  anchor: '100%',
                  items: [{
                      columnWidth: .17,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [beginDate]
                    }, {
                      columnWidth: .17,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [endDate]
                    }, {
                      columnWidth: .18,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [clearButton]
                    }]
                }]
            }]
        });
        
        //被选择的订单号。
        var saleOrderId;

        var saleOrderAuditDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderModel',
          proxy : {
            type : 'ajax',
            url : '/sale_order/getAllSaleOrdersByAuditStatus',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : pageSize,
          autoLoad : true
        });
        
        
        //生成要订单的产品的Grid
        var saleOrderAuditGrid = Ext.create('Ext.grid.Panel', {
          title:'销售订单审批列表',
          store: saleOrderAuditDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '公司',
              dataIndex: 'company_name',
              width: 50
            },{
              header: '销售订单单号',
              dataIndex: 'order_number',
              width: 210
            },{
              header: '客户',
              dataIndex: 'customer_name',
              width: 180
            },{
              header: '总金额',
              dataIndex: 'amount',
              renderer: moneyPrivilegeRender,
              align: 'right',              
              width: 85
            },{
              header: '发货日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),width: 80
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 80
            },{
              header: '授信折扣',
              dataIndex: 'unsafe_discount',
              width: 65,
              renderer: yesOrNoRender
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 50
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 150
            },{
              header: '处理状态',
              dataIndex: 'deal_with_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '出库状态',
              dataIndex: 'store_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '仓库操作员',
              dataIndex: 'delivery_employee_name',
              hidden:true,
              width: 80
            },{
              header: '销售类型',
              dataIndex: 'sale_type',
              width: 70
            },{
              header: '销售策略',
              dataIndex: 'sales_strategy',
              width: 80
            },{
              header: '备注',
              dataIndex: 'description',
              width: 170
            },{
              header: '出库说明',
              dataIndex: 'delivery_desc',
              width: 170
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          //renderTo: 'order_sale_list_grid',
          width: screenWidth-165,
          height: 315,
          frame: true,
          bbar: Ext.create('Ext.toolbar.Paging', {
            pageSize: pageSize,
            store: saleOrderAuditDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })       
        });

        saleOrderAuditGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          saleOrderId = record.get('id');
          var unsafeDiscount = record.get('unsafe_discount');

          auditOrder.setDisabled(true);
          auditOrderForCancel.setDisabled(true);
          auditEditOrder.setDisabled(true);
          
          var auditingStatus = record.get('auditing_status');
          if (auditingStatus === "申请新增"){
            auditOrder.setDisabled(false);
          }
          if (auditingStatus === "申请撤消"){
            auditOrderForCancel.setDisabled(false);
          }
          if (auditingStatus === "申请变更"){
            auditEditOrder.setDisabled(false);
          }

          //折扣授信的记录，如果没有权限，就设置为灰色。
          if (unsafeDiscount == '是' && unsafeDiscountAudit == '否'){
            auditOrder.setDisabled(true);
            auditEditOrder.setDisabled(true);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var auditOrder = Ext.create('Ext.menu.Item', {
          text: '【审批销售订单】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });

        var auditEditOrder = Ext.create('Ext.menu.Item', {
          text: '【审批销售订单变更】',
          //iconCls: 'album',
          handler: rightMenuAuditForEditFn
        });
        
        var auditOrderForCancel = Ext.create('Ext.menu.Item', {
          text: '【审批销售订单  <em style=color:red>(撤消)</em>】',
          //iconCls: 'album',
          handler: rightMenuAuditForCancelFn
        });

        function rightMenuAuditFn(){
          saleOrderAuditWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 1105,
            height: 590,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '销售订单审批',
            autoLoad: {
              url: "/sale_order/auditSaleOrder?sale_order_id="+saleOrderId,
              scripts: true
            }
          });
          saleOrderAuditWin.on("destroy",function(){
            saleOrderAuditDS.load();
          });
          saleOrderAuditWin.resizable = false;
          saleOrderAuditWin.show();
        }

        //审批调整销售订单
        function rightMenuAuditForEditFn(){
          saleOrderAuditForEditWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 1095,
            height: 615,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '审批销售订单<em style=color:red>(变更)</em>',
            autoLoad: {
              url: "/sale_order/auditSaleOrderForEdit?sale_order_id="+saleOrderId,
              scripts: true
            }
          });
          saleOrderAuditForEditWin.on("destroy",function(){
            saleOrderAuditDS.load();
          });
          saleOrderAuditForEditWin.resizable = false;
          saleOrderAuditForEditWin.show();
        }
        
        function rightMenuAuditForCancelFn(){
          saleOrderAuditForCancelWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 965,
            height: 590,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '销售订单审批<em style=color:red>(撤消)</em>',
            autoLoad: {
              url: "/sale_order/auditSaleOrderForCancel?sale_order_id="+saleOrderId,
              scripts: true
            }
          });
          saleOrderAuditForCancelWin.on("destroy",function(){
            saleOrderAuditDS.load();
          });
          saleOrderAuditForCancelWin.resizable = false;
          saleOrderAuditForCancelWin.show();
        }
        
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [auditOrder,auditEditOrder,auditOrderForCancel]
        });

        saleOrderAuditGrid.on('itemdblclick', function(gridPanel, record,item,index,e,options){
          saleOrderId = record.get('id');
          var saleOrderNumber = record.get('order_number');
          //saleOrderAuditDS.load();
          saleOrderDetailDS.removeAll();
          saleOrderDetailDS.load({params:{'sale_order_id': saleOrderId}});
          saleOrderDetailGrid.setTitle("销售订单："+saleOrderNumber+"，的详细信息");
        });

        //--------------------------销售订单详细，双击后可显示数据----------------
        //销售订单Grid Store Detail
        var saleOrderDetailDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderDetailModel',
          proxy : {
            type : 'ajax',
            url : '/sale_order/getSaleOrderDetailsBalanceById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });

        //生成要订单的产品的Grid
        var saleOrderDetailGrid = Ext.create('Ext.grid.Panel', {
          title:'订单详细列表',
          store: saleOrderDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 270
            },{
              header: '编号',dataIndex: 'product_number',width: 100
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 70
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 60
            },{
              header: '单价',
              dataIndex: 'unit_price',
              align: 'right',
              renderer: moneyPrivilegeRender,
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 70
            },{
              header: '折扣',
              dataIndex: 'discount',
              renderer: moneyPrivilegeRender,
              renderer: Ext.util.Format.numberRenderer('0,000.000000'),
              width: 70
            },{
              header: '总价',
              dataIndex: 'amount',
              align: 'right',
              renderer: moneyPrivilegeRender,
              width: 80
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }],
          loadMask: true,
          width: screenWidth-165,
          height: dgHeight,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-160,
          height: totalHeight,autoScroll:true,
          frame:false,
          layout: {
            type: 'table',
            columns:1
          },
          items:[queryFormPanel,saleOrderAuditGrid,saleOrderDetailGrid]
        });
        workbenchPanel.render("order_sale_audit_list_workbench");
      })
      
    </script>
  </head>
  <body>
    <div id="order_sale_audit_list_workbench"></div>
  </body>
</html>
